package com.turnengine.client.global.error.command;

import com.javabi.command.IExecutableCommand;
import com.javabi.command.IExecutableCommandResponse;
import com.javabi.command.serializer.AbstractCommandDataSerializer;
import com.javabi.common.io.data.IDataReader;
import com.javabi.common.io.data.IDataWriter;
import com.turnengine.client.local.error.bean.ILocalTrace;
import com.turnengine.client.local.error.bean.LocalTraceSerializer;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

/**
 * The Get Command Stack Trace List Data Serializer.
 */
public class GetCommandStackTraceListDataSerializer extends AbstractCommandDataSerializer<List<ILocalTrace>> implements IGetCommandStackTraceListDataSerializer {

	/**
	 * Creates a new Get Command Stack Trace List Data Serializer.
	 */
	public GetCommandStackTraceListDataSerializer() {
		super(GetCommandStackTraceList.COMMAND_ID);
	}

	@Override
	public List<ILocalTrace> readReturnValue(IDataReader reader) throws IOException {
		List<ILocalTrace> value = reader.readObjectCollection(new ArrayList<ILocalTrace>(), true, false, new LocalTraceSerializer());
		return value;
	}

	@Override
	public void writeReturnValue(IDataWriter writer, List<ILocalTrace> value) throws IOException {
		writer.writeObjectCollection(value, true, false, new LocalTraceSerializer());
	}

	@Override
	public IExecutableCommand<List<ILocalTrace>> readRequest(IDataReader reader) throws IOException {

		// Read fields
		long loginId = reader.readLong();
		long from = reader.readLong();
		long to = reader.readLong();

		// Create object
		GetCommandStackTraceList object = new GetCommandStackTraceList();
		object.setLoginId(loginId);
		object.setFrom(from);
		object.setTo(to);
		return object;
	}

	@Override
	public void writeRequest(IDataWriter writer, IExecutableCommand<List<ILocalTrace>> argument) throws IOException {
		GetCommandStackTraceList object = (GetCommandStackTraceList) argument;

		// Get fields
		long loginId = object.getLoginId();
		long from = object.getFrom();
		long to = object.getTo();

		// Write fields
		writer.writeLong(loginId);
		writer.writeLong(from);
		writer.writeLong(to);
	}

	@Override
	public IExecutableCommandResponse<List<ILocalTrace>> newResponse() {
		return new GetCommandStackTraceListResponse();
	}
}
